<!--其它操作页面 -->
<!--其它操作》库存录入 -->
<div id="StockCheckIn" class="hide">
    <form class="layui-form layui-form-pane" lay-verify="" action="" method="get">
        <div class="layui-form-item pt20 pl20">
            <div align="center">
                {{#  for(var i in d.list){ }}
                <label class="layui-btn layui-btn-danger show_code_input" size_name="{{ d.list[i].size_name }}">{{ d.list[i].size_name }}</label>
                {{#  } }}
            </div>
            <div class="scan_code hide">
                <div class="dress_line_code">
                    当前录入的尺码：<span class="size_name_span">___</span>，数量：<span class="checkInNumSpan">0</span>件 (注意：一个条码为一件)<br />
                    <textarea name="dress_line_code" id="dress_line_code" class="layui-area code_input" lay-verify="" placeholder="扫服装条码"></textarea>
                </div>
                <div class="defective_reason">次品原因：<input type="text" name="defective_reason" value="" placeholder="输入次品原因(非必填)">
                    <input type="hidden" name="stocktype" class="stocktype_input" value="">
                </div>
                <div class="submit_block">
                    <button class="layui-btn" lay-submit lay-filter="stock-out-in" >确认入库</button>
                </div>
            </div>
            <div class="checkin_ret hide">处理结果：尺码：<span class="size_name_span"></span>，数量：<span class="checkInNumSpan">0</span>件，入库成功；<br />
                当前订单该尺码总库存：<span class="total_stock"></span>件<br />
                可选择其它尺码继续录入</div>
            <div>
                &nbsp;
            </div>
            <input type="hidden" name="order_id" value="{{ d.order_id }}">
            <input type="hidden" name="url" value="{{ d.url }}">
            <input type="hidden" name="size_name" class="size_name_input" value="" lay-reqText="尺码请选择">
            <input type="hidden" name="number" value="" lay-verify="required" lay-reqText="请扫描服装条码"  class="layui-input checkinNum">
            <div>&nbsp;</div>
        </div>
    </form>
</div>
<!--尾部完成 -->
<div id="doneTpl" class="hide">
    <form class="layui-form layui-form-pane" lay-verify="" action="" method="get">
        <div class="layui-form-item pt20 pl20">
            {{#  for(var i in d.list){ }}
            <div class="layui-inline">
                <label class="layui-form-label">{{ d.list[i].size_name }}</label>
                <div class="layui-input-inline">
                    <input type="text" name="number[{{ d.list[i].mid }}]" value="{{# if(d.list[i].jiaohuo_tail_count>0){ }}{{ d.list[i].jiaohuo_tail_count }}{{#  } }}" lay-verify="number|required" placeholder="输入数量" class="layui-input">
                </div>
            </div>
            {{#  } }}

            <input type="hidden" name="order_id" value="{{ d.order_id }}">
            <input type="hidden" name="url" value="{{ d.url }}">
            <div class="layui-inline fr">
                <button class="layui-btn layui-btn-normal" lay-submit lay-filter="save" >保存</button>
                <button class="layui-btn" lay-submit lay-filter="done" >确认并完成</button>
            </div>
        </div>
    </form>
</div>
<!--调用库存 -->
<div id="StockCheckOut" class="hide">
    <form class="layui-form layui-form-pane" lay-verify="" action="" method="get">
        <div class="layui-form-item pt20 pl20">
            <div class="scan_code">
                <div class="dress_line_code">
                    请扫描要出库的服装：(注：一行一个服装条码)<br />
                    <textarea name="out_dress_line_code" id="out_dress_line_code" class="layui-area out_code_input" lay-verify="" placeholder="扫描要出库的服装条码"></textarea>
                    <input type="hidden" name="sku_stock" class="sku_stock" value='{{d.sku_stock}}'>
                </div>
                <div class="scan_code_block">验证提示：<span class="scan_code_ret"></span></div>
                <div class="submit_block">
                    <button class="layui-btn" lay-submit lay-filter="stock-out-out" >确认出库</button>
                </div>
            </div>
            <div class="checkin_ret hide">处理结果：<span class="act_ret_str"></span>出库成功；<br /></div>
            <div>
                &nbsp;
            </div>
            <input type="hidden" name="order_id" value="{{ d.order_id }}">
            <input type="hidden" name="url" value="{{ d.url }}">
            <input type="hidden" name="size_name" class="size_name_input" value="" lay-reqText="尺码请选择">
            <input type="hidden" name="number" value="" lay-verify="required" lay-reqText="请扫描要出库的服装条码"  class="layui-input outcheckinNum">
            <div>&nbsp;</div>
        </div>
    </form>
</div>
<!--调用库存 -->
<div id="stockOutInTpl" class="hide">
    <form class="layui-form layui-form-pane" lay-verify="" action="" method="get">
        <div class="layui-form-item pt20 pl20">
            {{#  for(var i in d.list){ }}
            <div class="layui-inline">
                <label class="layui-form-label">{{ d.list[i].size_name }}</label>
                <div class="layui-input-inline">
                    <input type="text" name="number[{{ d.list[i].mid }}]" value="" lay-verify="number|required" placeholder="输入数量" class="layui-input">
                </div>
            </div>
            {{#  } }}

            <input type="hidden" name="order_id" value="{{ d.order_id }}">
            <input type="hidden" name="url" value="{{ d.url }}">
            <div class="layui-inline fr">
                <button class="layui-btn" lay-submit lay-filter="stock-out-in" >确认</button>
            </div>
        </div>
    </form>
</div>
<!--加入成品统计 -->
<div id="defectiveTpl" class="hide">
    <form class="layui-form layui-form-pane" lay-verify="" action="" method="get">
        <div class="layui-form-item pt20 pl20">
            <div class="layui-inline">
                <label class="layui-form-label">订单总入仓数：</label>
                <div class="layui-input-inline">
                    <input type="text" name="people_in_store_count" value="{{d.in_store_count}}" style="width:120px;display:inline;" placeholder="输入数量" class="layui-input">&nbsp;件
                </div>
            </div>
            <div style="margin-top:5px;color:#666666;margin-bottom:15px;">注：不填总入仓数则统计所有子单入仓数</div>

            <input type="hidden" name="order_id" value="{{ d.order_id }}">
            <input type="hidden" name="url" value="{{ d.url }}">
            <div class="layui-inline fr">
                <button class="layui-btn" lay-submit lay-filter="stock-defective" >确认</button>
            </div>
        </div>
    </form>
</div>

<script type="text/javascript" >
    layui.use([ 'element', 'layer', 'laytpl', 'tool','laydate','table','form','flow'], function() {
        var element = layui.element;
        var layer = layui.layer;
        var $ = layui.jquery;
        var form = layui.form;
        var laytpl = layui.laytpl,laydate=layui.laydate ,table = layui.table;
        var flow = layui.flow;

        //库存录入
        var layerStockOutIn = null;
        $(document).on('click','.tail-stock-outin',function(){
            //参数处理，，获取选择订单的数据
            var limit_one=select_limit_one();
            if(limit_one==false){
                layer.msg('只能选择一个订单处理', {icon: 2,anim: 6});
                return false;
            }
            var ord=get_select_data(1);
            var orderId = ord.id,title=$(this).attr('title'),stocktype=$(this).attr('stocktype');
            var orderNum=ord.client_ordernum;
            var url = "{:url('mp/Tail/stockCheckIn')}?order_id="+ord.id;
            var json = ord.osJson;
            //以下是搬过来代码：

            var d = [];
            d['list'] = eval("(" + json+ ")");
            d['url'] = url;
            d['order_id'] = orderId;
            $(".size_name_span").html("__");
            $(".size_name_input").val('');
            $(".scan_code").hide();
            $(".defective_reason").css("display",stocktype=='defective'?'block':'none');
            $(".stocktype_input").val(stocktype);
            var content = laytpl($('#StockCheckIn').html()).render(d);
            layerStockOutIn = layer.open({
                type: 1
                ,area: ['650px','350px']
                ,offset: '100px'
                //,shade:[0.8, '#393D49']
                ,anim: parseInt(Math.random()*(6+1),10)
                ,skin:'layui-layer-molv'
                ,title:title+'-当前入库的订单号：'+orderNum
                ,content: content
            });
        });

        //调用库存
        var sku_stock='';
        $(document).on('click','.tail-stock-out',function(){
            //参数处理，，获取选择订单的数据
            var limit_one=select_limit_one();
            if(limit_one==false){
                layer.msg('只能选择一个订单处理', {icon: 2,anim: 6});
                return false;
            }
            var ord=get_select_data(1);
            var orderId = ord.id;
            var orderNum=ord.client_ordernum
            var url = "{:url('mp/Tail/stockCheckOut')}?order_id="+ord.id;
            var json = ord.otherStockList_json;
            //以下是搬过来代码：
            // var orderId = $(this).attr('data-id');
            // var orderNum=$(this).attr('data-ordernum');
            // var url = $(this).attr('data-url');
            // var json = $(this).attr('data-json');
            var d = [];
            sku_stock = eval("(" + json+ ")");
            d['sku_stock']=json;
            d['url'] = url;
            d['order_id'] = orderId;
            $(".size_name_span").html("__");
            $(".size_name_input").val('');
            $(".scan_code").show();
            $(".checkin_ret").hide();
            $(".out_code_input").focus();
            var content = laytpl($('#StockCheckOut').html()).render(d);
            layerStockOut = layer.open({
                type: 1
                ,area: ['660px','320px']
                ,offset: '100px'
                //,shade:[0.8, '#393D49']
                ,anim: parseInt(Math.random()*(6+1),10)
                ,skin:'layui-layer-molv'
                ,title:'库存调用-你选择占用库存的订单号：'+orderNum
                ,content: content
                ,success:function(layerdom,index){
                    $(".out_code_input").eq(1).focus();
                }
            });
        });
        $(document).on('input propertychange',".out_code_input", function() {
            var dress_line_code=$(this).val();
            if(dress_line_code==''){
                return;
            }
            var code_str=',';
            for(var i=0;i<sku_stock.length;i++){
                //alert(sku_stock[i]['dress_line_code']);
                code_str+=sku_stock[i]['dress_line_code']+",";
            }
            if(dress_line_code.length==17){
                dress_line_code=dress_line_code+"\n";
            }
            var codeNumbers=dress_line_code.split("\n");
            var last_input=codeNumbers[codeNumbers.length-1];
            var r, re;
            var re = eval('/'+last_input+'/ig');
            r = dress_line_code.match(re);   //匹配字符
            $(".scan_code_ret").html('')
            for(var i=0;i<codeNumbers.length;i++){
                if(code_str.indexOf(codeNumbers[i])!=-1){

                }else{
                    $(".scan_code_ret").eq(1).html("未找到"+codeNumbers[i]+"的库存信息或该条码不属于当前sku");
                }
            }
            if(codeNumbers[codeNumbers.length-1].length>=17){
                //$(".code_input").eq(1).val(dress_line_code+'\n');
            }
            $(".outcheckinNum").val(codeNumbers.length);
            $(".checkInNumSpan").html(codeNumbers.length);
        });

        $(document).on('click','.show_code_input',function(){
            $(".size_name_span").html($(this).attr('size_name'));
            $(".size_name_input").val($(this).attr('size_name'));
            $(".checkInNumSpan").html(0);
            $(".scan_code").show();
            $(".checkin_ret").hide();
            $(".code_input").eq(1).val('');
            $(".code_input").eq(1).focus();
        });
        $(document).on('input propertychange',".code_input", function() {
            var dress_line_code=$(this).val();
            if(dress_line_code==''){
                return;
            }
            var codes=dress_line_code.replace(/\n/g,"");
            var input_count=0;
            codes=codes.replace(/\r/g,"");
            var codeNumbers=codes.split(";");
            if(codeNumbers[codeNumbers.length-1].length>=17){
                $(".code_input").eq(1).val(codes+';');
                input_count=1;
            }
            $(".checkinNum").val(codeNumbers.length-1+input_count);
            $(".checkInNumSpan").html(codeNumbers.length-1+input_count);

        });

        form.on('submit(stock-out-in)', function (data) {
            console.log(data.field);
            var url = data.field.url;
            $.ajax({
                type: "POST",
                url: url,
                data: data.field,
                dataType:"json",
                success: function(res){
                    if (res.code == 1) {
                        $('#StockCheckIn form')[0].reset();
                        //layer.close(layerStockOutIn);
                        $(".total_stock").html(res.data.available_count);
                        $(".checkInNumSpan").html(res.data.checkin_count);
                        $(".scan_code").hide();
                        $(".checkin_ret").show();
                        $(".checkinNum").eq(0).val('');
                        $(".checkinNum").eq(1).val('');
                        layer.msg(res.msg, {icon: 1,end:function(){
                                //opTable.reload();
                            }});
                    } else {
                        layer.msg(res.msg, {icon: 2,anim: 6});
                    }
                }
            });

            return false;
        });

        form.on('submit(stock-out-out)', function (data) {
            console.log(data.field);
            var url = data.field.url;
            $.ajax({
                type: "POST",
                url: url,
                data: data.field,
                dataType:"json",
                success: function(res){
                    if (res.code == 1) {
                        $('#stockOutInTpl form')[0].reset();
                        $(".total_stock").html(res.data.available_count);
                        $(".scan_code").hide();
                        $(".checkin_ret").show();
                        $(".act_ret_str").html(res.data.act_ret_str);
                        layer.msg(res.msg, {icon: 1,end:function(){
                                // opTable.reload();
                                layer.close(layerStockOut);
                            }});
                    } else {
                        layer.msg(res.msg, {icon: 2,anim: 6});
                    }
                }
            });
            return false;
        });

        // 次品录入 --说明有误 ---加入成品统计
        var layerDefective = null;
        $(document).on('click','.tail-defective',function(){
            //参数处理，，获取选择订单的数据
            var limit_one=select_limit_one();
            if(limit_one==false){
                layer.msg('只能选择一个订单处理', {icon: 2,anim: 6});
                return false;
            }
            var ord=get_select_data(1);
            var orderId = ord.id;
            var url = "{:url('mp/Tail/setOrderFinish')}?order_id="+ord.id;
            var json = ord.osJson;
            var in_store_count=!ord.finishinfo ?'':ord.finishinfo.people_in_store_count;
            if(ord.tail_status!=1){
                layer.msg('尾部完成，才能加入成品统计', {icon: 2,anim: 6});
                return false;
            }
            //以下是搬过来代码：
            // var orderId = $(this).attr('data-id');
            // var url = $(this).attr('data-url');
            // var json = $(this).attr('data-json');
            // var in_store_count=$(this).attr('in_store_count');
            var d = [];
            d['list'] = eval("(" + json+ ")");
            d['url'] = url;
            d['order_id'] = orderId;
            d['in_store_count']=in_store_count;
            var content = laytpl($('#defectiveTpl').html()).render(d);
            layerDefective = layer.open({
                type: 1
                ,shade: 0
                ,offset: '100px'
                ,anim: parseInt(Math.random()*(6+1),10)
                ,skin:'layui-layer-molv'
                ,title:'确认加入成品统计'
                ,content: content
            });
        });
        form.on('submit(stock-defective)', function (data) {
            console.log(data.field);
            var url = data.field.url;
            $.ajax({
                type: "POST",
                url: url,
                data: data.field,
                dataType:"json",
                success: function(res){
                    if (res.code == 1) {
                        $('#defectiveTpl form')[0].reset();
                        layer.close(layerDefective);
                        layer.msg(res.msg, {icon: 1,end:function(){
                            // opTable.reload();
                        }});
                    } else {
                        layer.msg(res.msg, {icon: 2,anim: 6});
                    }
                }
            });

            return false;
        });

        // 尾部完成
        var layerDone = null;
        $(document).on('click','.tail-done',function(){
            //参数处理，，获取选择订单的数据
            var limit_one=select_limit_one();
            if(limit_one==false){
                layer.msg('只能选择一个订单处理', {icon: 2,anim: 6});
                return false;
            }
            var ord=get_select_data(1);
            if (ord.tail_status == 1) {
                layer.msg('只能手动改变成完成状态，输入的码数效的。后台已经定时处理这订单。', {icon: 2, anim: 6});
            }
            var orderId = ord.id;
            var url = "{:url('mp/Tail/done')}?order_id="+ord.id;
            var json = ord.osJson;
            //以下是搬过来代码：

            // var orderId = $(this).attr('data-id');
            // var url = $(this).attr('data-url');
            // var json = $(this).attr('data-json');
            var d = [];
            d['list'] = eval("(" + json+ ")");
            d['url'] = url;
            d['order_id'] = orderId;
            var content = laytpl($('#doneTpl').html()).render(d);
            layerDone = layer.open({
                type: 1
                ,shade: 0
                ,offset: '100px'
                ,anim: parseInt(Math.random()*(6+1),10)
                ,skin:'layui-layer-molv'
                ,title:'后整数录入'
                ,content: content
            });
        });
        // 尾部完成>尾部数据保存
        form.on('submit(save)', function (data) {
            var url = data.field.url;
            $.ajax({
                type: "POST",
                url: url,
                data: data.field,
                dataType:"json",
                success: function(res){
                    if (res.code == 1) {
                        $('#doneTpl form')[0].reset();
                        layer.close(layerDone);
                        layer.msg(res.msg, {icon: 1,end:function(){reloadTable();}});
                    } else {
                        layer.msg(res.msg, {icon: 2,anim: 6});
                    }
                }
            });
            return false;
        });

        // 尾部完成>尾部确认完成
        form.on('submit(done)', function (data) {
            var url = data.field.url;
            $.ajax({
                type: "POST",
                url: url+"&is_done=1",
                data: data.field,
                dataType:"json",
                success: function(res){
                    if (res.code == 1) {
                        $('#doneTpl form')[0].reset();
                        layer.close(layerDone);
                        layer.msg(res.msg, {icon: 1,end:function(){reloadTable();}});
                    } else {
                        layer.msg(res.msg, {icon: 2,anim: 6});
                    }
                }
            });
            return false;
        });

        //同步发货单 查看发货单
        $(document).on('click','.delivery-sync',function(){
            //参数处理，，获取选择订单的数据
            var limit_one=select_limit_one();
            if(limit_one==false){
                layer.msg('只能选择一个订单处理', {icon: 2,anim: 6});
                return false;
            }
            var ord=get_select_data(1);
            var url = "{:url('mp/Tail/deliverySync')}?order_id="+ord.id;
            var url2 = "{:url('mp/Tail/deliveryList')}?order_id="+ord.id;
            var daohuo_count_flag=0;
            var json=ord.osJson;
            var type=$(this).attr('data-option');

            var d=[];
            d['list'] = eval("(" + json+ ")");
            $.each( d['list'], function(i, n){
                if(n.daohuo_count>0){
                    daohuo_count_flag=1;
                    return true;
                }
            });
            //同步发货单
            if(type=='sync' && ord.delivery_sync_status!=2 && ord.tail_status==1 && daohuo_count_flag==1){
                $.ajax({
                    type: "POST",
                    url: url,
                    data: {},
                    dataType:"json",
                    success: function(res){
                        // console.log(res);
                        if (res.code == 1) {
                            layer.msg(res.msg, {icon: 1,end:function(){
                                    //opTable.reload();
                                }});
                        } else {
                            layer.msg(res.msg, {icon: 2,anim: 6});
                        }
                    }
                });
            }

            //查看发货单
            if(type=='find' && ord.is_delivery==1){
                $('.onclick-delivery-sync-hisi-iframe').attr('href',url2);
                $('.onclick-delivery-sync-hisi-iframe').trigger('click');
                return true;
            }
        });

    });

</script>